<template>
	<view>
		<view class="logo">
			<image src="/static/image/logo.jpg" mode=""></image>
		</view>
		<view class="name">小時候家政中心</view>
		<button type="primary" open-type="getUserInfo" @getuserinfo="getuserinfo">微信登录</button>
		<view class="text">登录代表您已同意<text>用户协议</text></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
			}
		},
		methods: {
			getuserinfo: function(res1) {
				// console.log(res1)
				var encryptedData = res1.detail.encryptedData;
				var iv = res1.detail.iv;
				wx.login({
					success: function(res2) {
						if (res2.code) {
							var code = res2.code;
							wx.request({
								url: 'https://ys.luowangtech.club/api/wx/login?code=' + code,
								success: (res3) => {
									// console.log(res3)
									 //记录到本地
								     uni.setStorageSync('sk', res3.data.data.session_key);
								     uni.setStorageSync('openid', res3.data.data.openid);
									//判断是否授权
									if (!res1.detail.iv) {
										uni.showToast({
											title: "您取消了授权,登录失败",
											icon: "none"
										});
										return false;
									}
									//以下步骤可以获取加密信息，需要授权
									//获取加密信息
									var sessionKey = uni.getStorageSync('sk');
									uni.request({
										method : "POST",
										url : 'https://ys.luowangtech.club/api/wx/decrypt',
										header : {'content-type':'application/x-www-form-urlencoded'},
										data : {
											appid : "wxcd417936b51ed32a",
											session_key : sessionKey,
											iv : iv,
											encrypteData : encryptedData
										},
										success:(res4)=>{
											console.log(res4);
											if(res4.statusCode==200){
												if(res4.data.status == 0){//登陆失败
													uni.showToast({
														title: res4.data.msg,
														icon: "none"
													});
													return;
												}
												//保存token
												uni.setStorageSync('token', res4.data.token);
												//保存头像
												uni.setStorageSync('avatar_url', res4.data.avatar_url);
												//保存昵称
												uni.setStorageSync('nickname', res4.data.nickname);
												//保存storageflag
												uni.setStorageSync('storageflag', '禁用');
												uni.showToast({
													title: res4.data.msg,
													icon: "success"
												});
												setTimeout(function(){
													// uni.navigateTo({
													// 	url: './mobile/index'
													// })
													uni.navigateBack({
														delta:1
													})
												},1000)
											}
										}
									})
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.logo {
		width: 112upx;
		height: 112upx;
		border: 2upx solid #eee;
		border-radius: 16upx;
		margin: 0 auto;
		margin-top: 180upx;
		overflow: hidden;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.name {
		text-align: center;
		font-size: 32upx;
		font-weight: bold;
		margin-top: 10upx;
		color: #323232;
	}

	button {
		width: 90%;
		margin-top: 200upx;
		margin-left: 5%;
		text-align: center;
		font-size: 30upx;
		color: #fff;
		height: 84upx;
		line-height: 84upx;
	}

	.text {
		position: fixed;
		bottom: 60upx;
		left: 50%;
		transform: translate(-50%, 0);
		font-size: 26upx;
		color: #979797;

		text {
			color: #1aad19;
			font-weight: bold;
		}
	}
</style>
